package com.jams.goodox.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.jams.goodox.R;
import com.jams.goodox.listen.DialogActionListener;
import com.jams.goodox.listen.SuccessListener;
import com.jams.goodox.widget.PromptBuilderDialog;

/**
 * Created by jams on 2017/5/2.
 * ***
 * Tel: ***
 * <p>
 * 统一对话框工具类
 */

public class DialogUtils {

    private static AlertDialog outLogin;

    /**
     * 显示删除提示对话框
     *
     * @param activity        当前页面
     * @param tip             提示内容
     * @param successListener 回调
     */
    public static void deleteConfirm(Activity activity, String tip, final SuccessListener successListener) {
        new PromptBuilderDialog(activity)
                .setTitle(R.string.tips)
                .setContent(tip)
                .setNegative(R.string.cancel)
                .setPositive(R.string.confirm)
                .setActionClickListener(new DialogActionListener() {
                    @Override
                    public void confirm(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        successListener.success();
                    }

                    @Override
                    public void cancel(DialogInterface dialog, int which) {

                    }
                })
                .show();
    }

    /**
     * 显示确认提示对话框
     *
     * @param activity        当前页面
     * @param tip             提示内容
     * @param successListener 回调
     */
    public static void confirm(Activity activity, String tip, final SuccessListener successListener) {
        new PromptBuilderDialog(activity)
                .setContent(tip)
                .setNegative(R.string.cancel)
                .setPositive(R.string.confirm)
                .setActionClickListener(new DialogActionListener() {
                    @Override
                    public void confirm(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        successListener.success();
                    }

                    @Override
                    public void cancel(DialogInterface dialog, int which) {

                    }
                })
                .show();
    }

    public static void showTokenMiss(final Context context, String msg) {
        if(context == null){
            return;
        }
        if(context instanceof Activity){
            if(((Activity) context).isFinishing() || ((Activity) context).isDestroyed())
                return;
        }
        if (outLogin != null) {
            outLogin.dismiss();
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(false)
                .setMessage(msg != null ? msg : "Token已过期，请重新登录！")
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        outLogin.dismiss();
                        Utils.logOut(context);
                    }
                });
        outLogin = builder.show();
    }
}
